// waveeditorView.cpp : implementation of the CWaveeditorView class
//

#include "stdafx.h"
#include "waveeditor.h"

#include "waveeditorDoc.h"
#include "waveeditorView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWaveeditorView

IMPLEMENT_DYNCREATE(MainTreeView, CTreeView)

BEGIN_MESSAGE_MAP(MainTreeView, CTreeView)
	//{{AFX_MSG_MAP(CWaveeditorView)
	ON_COMMAND(ID_UPDATE_SELECTED_SCRIPT, OnUpdateSelectedScript)
	ON_COMMAND(ID_SAVETOVECTORFILE, OnSavetovectorfile)
	ON_NOTIFY_REFLECT(NM_KILLFOCUS, OnKillfocus)
	ON_COMMAND(ID_ITEMINFO, OnIteminfo)
	ON_COMMAND(ID_EDITSCRIPTFILE, OnEditscriptfile)
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_ADD_SCRIPT_OR_LOG, OnAddScriptOrLog)
	ON_COMMAND(ID_DELETESCRIPT, OnDeletescript)
	ON_COMMAND(ID_SAVEALLTOVECTORFILE, OnSavealltovectorfile)
	ON_COMMAND(ID_FRAMEGRAPH, OnFramegraph)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CTreeView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWaveeditorView construction/destruction

MainTreeView::MainTreeView():ctree(GetTreeCtrl())
{
	// TODO: add construction code here

}

MainTreeView::~MainTreeView()
{
	delete myImage;
}

BOOL MainTreeView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style	|=	TVS_HASLINES |TVS_LINESATROOT |TVS_SHOWSELALWAYS|TVS_HASBUTTONS   ;
	return CTreeView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CWaveeditorView drawing

void MainTreeView::OnDraw(CDC* pDC)
{
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
//	MakeNewList();

}

void MainTreeView::OnInitialUpdate()
{
//	ctree.
	CTreeView::OnInitialUpdate();

	// TODO: You may populate your TreeView with items by directly accessing
	//  its tree control through a call to GetTreeCtrl().
	CFrameWnd* pFrame	=	GetParentFrame();
	pFrame->SetWindowText("Tree View");
	CBitmap bp[4];
	bp[0].LoadBitmap(IDB_SCRIPT);
	bp[1].LoadBitmap(IDB_FRAME);
	bp[2].LoadBitmap(IDB_COMPLEX);
	bp[3].LoadBitmap(IDB_GROUP);
	myImage= new CImageList();
	myImage->Create(16,16,ILC_COLOR8,0,4);
	myImage->Add(&bp[0],RGB(0,0,0));
	myImage->Add(&bp[1],RGB(0,0,0));
	myImage->Add(&bp[2],RGB(0,0,0));
	myImage->Add(&bp[3],RGB(0,0,0));
	ctree.SetImageList(myImage,TVSIL_NORMAL );
//	MakeNewList();
}

/////////////////////////////////////////////////////////////////////////////
// CWaveeditorView printing

BOOL MainTreeView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void MainTreeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void MainTreeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CWaveeditorView diagnostics

#ifdef _DEBUG
void MainTreeView::AssertValid() const
{
	CTreeView::AssertValid();
}

void MainTreeView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}

ZWaveeditorDoc* MainTreeView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ZWaveeditorDoc)));
	return (ZWaveeditorDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CWaveeditorView message handlers

int MainTreeView::MakeNewList()
{
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	if(pDoc->treewaschanged	==	false)
	{ 
		//return -1;
	}
	pDoc->treewaschanged	=	FALSE;
	HTREEITEM	temp		=	ctree.GetRootItem();
	DWORD		num			=	10;
	if(temp	!=	NULL)
	{
		num	=	ctree.GetItemData(temp);
	}
	pDoc->ExportTree(&ctree,num);
	ClearGarbage(&ctree,num);
/*	for(int i=0;i<pDoc->_script.GetSize();i++)
	{
		if(pDoc->_script[i]->GetScenarioStatus() == TRUE)
		{
			pDoc->_script[i]->ExportTree(&ctree,num);
		}
	}
*/	return 0;
}

void MainTreeView::ClearGarbage(CTreeCtrl* _tree, DWORD num)
{
	HTREEITEM	point		=	_tree->GetRootItem();
	while(point	!= NULL)
	{
		if(_tree->GetItemData(point) == num)
		{
			HTREEITEM	oldpoint	=	point;
			point					=	_tree->GetNextItem(point,TVGN_NEXT);
			_tree->DeleteItem(oldpoint);
		}
		else
		{
			HTREEITEM child	=	_tree->GetChildItem(point);
			if(child	!=	NULL)
			{
				ClearStep(_tree,child,num);
				point		=	_tree->GetNextItem(point,TVGN_NEXT);
			}
			else
			{
				point					=	_tree->GetNextItem(point,TVGN_NEXT);
			}
		}
	}
}

void MainTreeView::ClearStep(CTreeCtrl* _tree, HTREEITEM point, DWORD num)
{
	while(point	!= NULL)
	{
		if(_tree->GetItemData(point) == num)
		{
			HTREEITEM	oldpoint	=	point;
			point					=	_tree->GetNextItem(point,TVGN_NEXT);
			_tree->DeleteItem(oldpoint);
		}
		else
		{
			HTREEITEM child	=	_tree->GetChildItem(point);
			if(child	!=	NULL)
			{
				ClearStep(_tree,child,num);
				point		=	_tree->GetNextItem(point,TVGN_NEXT);
			}
			else
			{
				point					=	_tree->GetNextItem(point,TVGN_NEXT);
			}
		}
	}
}


void MainTreeView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	ChangeWindowTitle();
	MakeNewList();
}





void MainTreeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	// TODO: Add your specialized code here and/or call the base class
	ChangeWindowTitle();
	CTreeView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}

void MainTreeView::OnKillfocus(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	ChangeWindowTitle();
	*pResult = 0;
}


void MainTreeView::OnUpdateSelectedScript() 
{
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: Add your command handler code here
	HTREEITEM _item	=	ctree.GetSelectedItem( );
	pDoc->UpdateScriptFromSelection(_item);
}









void MainTreeView::OnSavetovectorfile() 
{
	// TODO: Add your command handler code here
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: Add your command handler code here
	HTREEITEM _item	=	ctree.GetSelectedItem( );
	pDoc->OnSavetovectorfile(&ctree,_item);
}




void MainTreeView::OnIteminfo() 
{
	// TODO: Add your command handler code here
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: Add your command handler code here
	HTREEITEM _item	=	ctree.GetSelectedItem( );
	pDoc->OnItemInfo(&ctree,_item);
}

void MainTreeView::OnEditscriptfile() 
{
	// TODO: Add your command handler code here
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: Add your command handler code here
	HTREEITEM _item	=	ctree.GetSelectedItem( );
	pDoc->OnEditscriptfile(&ctree,_item);
}

void MainTreeView::ChangeWindowTitle()
{
	CFrameWnd* pFrame	=	GetParentFrame();
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CString	name	=	"Tree View (";
	name			+=	pDoc->GetTitle()+ ")";
	pFrame->SetWindowText(name);

}



void MainTreeView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: Add your command handler code here
	HTREEITEM _item	=	ctree.GetSelectedItem( );
	pDoc->OnGraphViewFromTree(&ctree,_item);
//	UpDateTree	=	FALSE;
	*pResult = 0;
}


void MainTreeView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	UINT uFlags;
	HTREEITEM hItem = ctree.HitTest(point, &uFlags);
	if ((hItem != NULL) && (TVHT_ONITEM & uFlags))
	{
	   ctree.SelectItem(hItem);
	}
	CMenu contextmenu;
	contextmenu.LoadMenu(IDR_WAVEEDTYPE);
	RECT rect;
	GetWindowRect(&rect);
	contextmenu.GetSubMenu(1)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x+rect.left,point.y+rect.top,this);
	CTreeView::OnRButtonDown(nFlags, point);
}

void MainTreeView::OnAddScriptOrLog() 
{
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: Add your command handler code here
	pDoc->OnAddScriptOrLog();	
}

void MainTreeView::OnDeletescript() 
{
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: Add your command handler code here
	HTREEITEM _item	=	ctree.GetSelectedItem( );
	pDoc->OnDeleteselectedscript(_item);
	
}

void MainTreeView::OnSavealltovectorfile() 
{
	// TODO: Add your command handler code here
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: Add your command handler code here
	pDoc->OnSavealltovectorfile();
	
}

void MainTreeView::OnFramegraph() 
{
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: Add your command handler code here
	HTREEITEM _item	=	ctree.GetSelectedItem( );
	pDoc->OnGraphViewFromTree(&ctree,_item);
}

